package com.v5.mr.reveredindex;

import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class RevertedIndexCombiner extends Reducer<Text, Text, Text, Text> {

	@Override
	protected void reduce(Text k21, Iterable<Text> v21, Context context)
			throws IOException, InterruptedException {
		// 求和：对同一个文件中的单词计数
		long total = 0;
		for(Text t:v21){
			total = total + Long.parseLong(t.toString());
		}
		
		//k21:  love:data01.txt
		String str = k21.toString();
		//找到冒号的位置
		int index = str.indexOf(":");
		
		String word = str.substring(0, index);//单词
		String fileName = str.substring(index + 1);//文件名
		
		//输出
		context.write(new Text(word), new Text(fileName+":"+total));
	}

}
